﻿@page "/users/clients"
@attribute [Authorize]
@inject IHttpService _httpService

<MRow>
    <MCol Cols="12" Lg="6" Xl="4">
        <MCard Class="list-card">
            <MCardTitle>应用列表</MCardTitle>
            <MCardText>
                创建第三方应用后，你可以将CnGal资料站作为第三方登入提供商加入到你的网站、APP中，之后你的用户可以使用CnGal资料站的账户登入你的应用
            </MCardText>
            <MCardActions>
                <MSpacer></MSpacer>
                <MButton Color="primary"
                         Dark
                         OnClick="OnClickAdd">
                    <MIcon Left>mdi-plus</MIcon>
                    创建
                </MButton>
            </MCardActions>
        </MCard>
    </MCol>
    @if (Model == null)
    {
        <MCol Cols="12" Lg="6" Xl="4">
            <CnGalWebSite.Components.Progressings.ProgressingCard Class="h-100" />
        </MCol>
    }
    else
    {
        @foreach (var item in Model)
        {
            <MCol Cols="12" Lg="6" Xl="4">
                <CnGalWebSite.IdentityServer.Admin.Shared.Components.Clients.OverviewCard Model="item" Class="list-card" OnEdited="OnEdited" AllowedScopes="_scopes"/>
            </MCol>
        }
    }

</MRow>

<MDialog @bind-Value="_editDialog"
         MaxWidth="1200">
    <CnGalWebSite.IdentityServer.Admin.Shared.Components.Clients.EditCard Id="_editedItemId" OnEdited="OnEdited" @ref="editCard" AllowedScopes="_scopes" />
</MDialog>

@code {
    List<UserClientOverviewModel> Model;

    private bool _editDialog;
    private int _editedItemId;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    CnGalWebSite.IdentityServer.Admin.Shared.Components.Clients.EditCard editCard;
    List<KeyValuePair<string, string>> _scopes;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _scopes = (await _httpService.GetAsync<IEnumerable<KeyValuePair<string, string>>>("api/identityresources/all")).ToList();
            _scopes.AddRange(await _httpService.GetAsync<IEnumerable<KeyValuePair<string, string>>>("api/apiscopes/all"));

            await Refresh();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取所有用户角色失败");
        }

    }

    public async Task Refresh()
    {
        try
        {
            Model = null;
            StateHasChanged();
            Model = await _httpService.GetAsync<List<UserClientOverviewModel>>("api/clients/list");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取客户端列表失败");
        }
    }

    public async Task OnClickAdd()
    {
        _editedItemId = 0;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public async Task OnEdited()
    {
        _editDialog = false;
        await Refresh();
    }
}
